package cn.tedu.micro.demo.rabbitmq.controller;

import cn.tedu.micro.demo.rabbitmq.pojo.UserMsg;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /*
        方式:get
        地址:send
        参数:String msg 想要发送的消息内容
        返回:"ok"
     */
    @GetMapping("/send")
    public String sendMsg(String msg){
        //如果在业务发送消息 需要关注补充 properties headers数据,不能发送Object类型 自己
        //封装Message
        //priority 优先级
        //appId 生产消息应用Id
        /*MessageProperties props
                = MessagePropertiesBuilder.newInstance().build();
        props.setAppId("115");
        Message message=new Message(msg.getBytes(),props);*/
        //channel.basicPublish("demo-ex","",props,msg.getBytes());
        rabbitTemplate.convertAndSend("demo-ex","",msg);
        return "ok";
    }
    @GetMapping("/send/user")
    public String sendUserMsg(UserMsg userMsg){
        rabbitTemplate.convertAndSend("demo-ex","",userMsg);
        return "ok";
    }
}
